package share

import (
	"jslog/common/model"
	commSer "jslog/common/service"
	"jslog/example"
	"jslog/service"
	"jslog/system/middleware"
	"jslog/util"

	"github.com/gin-gonic/gin"
)

type Login struct {
	service.Comon
}

type UserInfo struct {
	model.User
	Authorization string
}

// 登录
func (s Login) Login(c *gin.Context) {

	account := c.DefaultPostForm("account", "")
	captcha := c.DefaultPostForm("captcha", "")
	captcha_id := c.DefaultPostForm("captcha_id", "")
	captcha = "1"
	captcha_id = "1"
	if captcha == "" {
		s.Error(0, "", "验证码不能为空", c)
		return
	}
	if captcha_id == "" {
		s.Error(0, "", "验证信息错误", c)
		return
	}

	graphiCaptcha := new(commSer.GraphiCaptcha)
	reVerify := graphiCaptcha.Verify(captcha_id, captcha, c)
	if reVerify == false {
		// s.Error(0, "", "验证失败", c)
		// return
	}

	if account == "" {
		s.Error(0, "", "账号不能为空", c)
		return
	}
	passwd := c.DefaultPostForm("passwd", "")
	if passwd == "" {
		s.Error(0, "", "密码不能为空", c)
		return
	}
	uInfo := new(model.User)
	uInfo.Phone = account
	userModel := new(model.UserModel)

	var userInfo UserInfo

	reUserDetail, _, reExist := userModel.Detail(0, uInfo)
	if !reExist {
		s.Error(0, "", "未注册, 请先注册", c)
		return
	}
	passWdCode := make(map[string]string)
	passWdCode["salt"] = reUserDetail.Salt
	passWdCode["passwd"] = passwd
	rePasswd := util.Passwd(0, passWdCode)
	if reUserDetail.Passwd == rePasswd {

		var tokenArr = make(map[string]string)
		tokenArr["uid"] = util.Uint64ToStr(reUserDetail.Id)
		tokenArr["username"] = reUserDetail.Username
		tokenArr["phone"] = account
		tokenArr["issuer"] = example.JSLOG_CONF.JWT.Issuer
		userInfo.Authorization = middleware.GetToken(tokenArr)
		var randstr string = util.RandStr()
		userInfo.Identify_comm = util.Md5(randstr + reUserDetail.Phone + util.Uint64ToStr(reUserDetail.Id) + userInfo.Authorization)

		userInfo.Nickname = reUserDetail.Nickname
		userInfo.Avatar = reUserDetail.Avatar
		userInfo.Id = reUserDetail.Id
		userInfo.Phone = reUserDetail.Phone
		userInfo.Realname = reUserDetail.Realname
	} else {
		s.Error(0, "", "账号或密码错误", c)
		return
	}
	s.Succee(userInfo, c)
	return
}

// 注册
func (s Login) Register(c *gin.Context) {

	account_type := c.DefaultPostForm("account_type", "")
	if account_type == "" {
		s.Error(0, nil, "账号类型错误", c)
	}
	account := c.DefaultPostForm("account", "")
	if account == "" {
		s.Error(0, nil, "账号不能为空", c)
	}
	var bol bool

	userModelObj := new(model.UserModel)
	userDbObj := new(model.User)

	if account_type == "mail" {
		userDbObj.Email = account
	} else if account_type == "phone" {
		userDbObj.Phone = account
	}
	_, _, bol = userModelObj.Detail(0, userDbObj)
	if bol {
		s.Error(0, nil, "账号已存在", c)
		return
	}

	passwd := c.DefaultPostForm("passwd", "")
	if passwd == "" {
		s.Error(0, nil, "密码不能为空", c)
	}

	userAdd := make(map[string]string)
	userAdd["account_type"] = account_type
	userAdd["account"] = account
	userAdd["passwd"] = passwd
	userAdd["lastloginip"] = service.GetClientIP(c.Request)
	result := userModelObj.Auserdd(userAdd)
	if result {
		s.Succee("注册成功", c)
	} else {
		s.Error(0, "", "注册失败", c)
	}
}
